<?php
require 'config.php';
session_start();
error_reporting(5);
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(0);
ob_start();
// 防止header语句不跳转；
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="zh-cn" xmlns="http://www.w33.org/1999/xhtml"></html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	<title>旧衣物回收</title>
	<link rel="stylesheet" type="text/css" href="./css/detail.css" charset=utf-8>
	<link rel="stylesheet" type="text/css" href="./css/list.css" charset=utf-8>
</head>
<body>

<?php
require 'top.php';
    $goodskey_id=$_GET['key_id'];
    $query = "SELECT * FROM goods WHERE goodsid=$goodskey_id AND state='0' ";
    $result = mysqli_query($config,$query) or die('SQL语句有误：'.mysqli_error());
    $row=mysqli_fetch_array($result);


// if(isset($_SESSION['userurl']))   
// 			// 如果获取到这个userurl，即这个状态存在header("location:$url")
// 			{$url = $_SESSION['userurl'];header('Refresh: 1;url=location:$url');}
// 		     //给它定义一个变量，获取到userurl，在跳转到这个变量的地址 
// 		else{echo('<script type="text/javascript">
// 			window.location.href = "http://localhost/school/index.php";</script>');}


if(isset($_GET['sessionId'])){
// 此处语句意思是，保证在点击购物车获得$_GET['sessionId']的时候，先验证是否是登录状态；
// 此处语句可以应用的页面很多，比如购物车，管理，发布等未登录时需要验证的页面

if(!isset($_SESSION['login'])){
	echo ('<script type="text/javascript">alert("请先登录！");
		window.location.href = "login/index.php";
		</script>');exit();
	// header('Location:http://localhost/school/login/index.php');
	ob_end_flush();
}



if(isset($_GET['delete'])){
	unset($_SESSION['login']);
	header('Location:http:login/index.php');
}

// // // 将本页面进行session，以便在登录之后，跳回$_SESSION['userurl']页面，重点
 $_SESSION['userurl']=$_SERVER['REQUEST_URI'];
//  // 验证完成，继续下面的操作

// // 买家信息

$email=$_SESSION['login'];
$query_email="SELECT * FROM users WHERE `uemail`='$email'";
$result_email=mysqli_query($config,$query_email);
$row_email=mysqli_fetch_array($result_email);
// echo $row_email['uemail'];
$buyer_email=$row_email['userid'];


// 卖家信息
$conSaler = "SELECT * FROM users WHERE userid='".$row['userid']."'";
$queSaler = mysqli_query($config,$conSaler);
// 查询代码错误
if (!$queSaler) {
    printf("Error: %s\n", mysqli_error($config));
    exit();
}

$reSaler = mysqli_fetch_array($queSaler);
$saler_email=$reSaler['userid'];



// 同一个人不能添加已添加过的商品
$goodskey_id=$_GET['key_id'];
$query_cart="SELECT * FROM carts WHERE `goodsid`='$goodskey_id' AND `buyerid`='$buyer_email'";
$result_cart=mysqli_query($config,$query_cart);
if($result_cart){
	$row_cart=mysqli_fetch_array($result_cart);
	if($row_cart){
		echo ('<script type="text/javascript">alert("您已添加此商品！");
			window.location.href = "detail.php?key_id='.$row_cart['goodsid'].'";</script>');
		exit();
	}
}

// 买家不能添加自己卖的商品
if($row_email['userid']==$reSaler['userid']){
	echo ('<script type="text/javascript">alert("不能添加自己的商品！");
		window.location.href = "index.php";
		</script>');exit();
}
// 判断库存是否为0
// $query_kucun="SELECT * FROM goods WHERE `goodsid`='$goodsid'";
// $kucun_result=mysql_query($query_kucun);
// $data=mysql_fetch_array($kucun_result);
// if ($data[kucun]<=0) {
// 	echo "<script>alert('该商品已售罄！')"
// }
if($row['kucun']<=0){
	echo ('<script type="text/javascript">alert("该商品已售罄！");
		window.location.href = "index.php";
		</script>');exit();
}

// 生成随机数
$rand=rand(12356492,99999999);


// 获取时间
$pub_time=Date('Y-m-d H:i:s');

// 未添加则插入数据表
$sql = "INSERT INTO carts (orderid,buyerid,buyername,salerid,saler_name,saler_tel,saler_address,cost,state,goodsid,goodsname,bigtypeid,smalltypeid,picture,uemail,pub_time,bianhao)
values (null,'".$row_email['userid']."','".$row_email['uname']."','".$saler_email."','".$reSaler['uname']."','".$reSaler['uphone']."','".$reSaler['uaddress']."','".$row['price']."','0','".$row['goodsid']."','".$row['goodsname']."','".$row['bigtypeid']."','".$row['smalltypeid']."','".$row['picture']."','".$row_email['uemail']."','".$pub_time."','".$rand."')";
mysqli_query($config,$sql) or die ('新增错误：'.mysqli_error($config));
// $query2="UPDATE.goods.set.state='1'.WHERE.goodsid='".$_GET['sessionId']."'";
// $result2=mysql_query($query2) or die ('SQL语句出错：'.mysql_error());



echo ('<script type="text/javascript">alert("添加成功！");
			window.location.href = "detail.php?key_id='.$goodskey_id.'";</script>');
		exit();

 
}
?>


<div class="template">
	<div class="title"><?php echo $row['goodsname'];?></div>
	<div class="subtitle">校园里最靠谱的二手衣物交易平台</div>
	<!-- 关联部分开始 -->
	<div class="category">
		<!-- 这里用js将ul关联 -->
		<ul>
			<li class="active">详情</li>
			<li>评论</li>
			<li>推荐</li>
		</ul>
	</div>
	<!-- 关联部分结束 -->
	<!-- 详情页面开始 -->
	<div class="content active">
		<!-- 左边图片部分 -->
		<div class="content-left">
			<ul>
				<li>
					<div class="img" style="float: left;">
						<img src=".\images\goods\<?php echo $row['bigtypeid']?>\<?php echo $row['smalltypeid'] ?>\<?php echo $row['picture'];?>">
					</div>
					<!-- <a href="#top"><div class="bg">
						<img src="./images/logo.jpg" />
					</div></a> -->
				</li>
			</ul>
		</div>
		<!-- 左边图片部分结束 -->
		<!-- 右边信息部分开始 -->
		<div class="content-right">
			<table class="index-list">
				<!-- 第一行 -->
				<tr>
					<td>名称</td>
					<td><a href="#"><?php echo $row['goodsname'];?></a></td>
					<td>售价</td>
					<td><a href="#">￥<span style="color: #ff6600;font-weight: bold;"><?php echo $row['price'];?></span><span style="margin-left: 15px;">￥<s style="color: #ff6600;font-weight: 300;"><?php echo $row['yuanjia'];?></s></span></a></td>
				</tr>
				<!-- 第二行 -->
				<tr>
					<td>类型</td>
					<td><a href="#"><?php echo $row['leixing'];?></a></td>
					<td>程度</td>
					<td><a href="#"><?php echo $row['zhuangtai'];?></a></td>
				</tr>
				<!-- 第三行 -->
				<tr>
					<td>买家昵称</td>
					<td><a href="#"><?php 
					$con1="SELECT * FROM users WHERE userid='" .$row['userid']."'";
					$que1 = mysqli_query($config,$con1);
					$saler = mysqli_fetch_array($que1);
					if($saler){echo $saler['uname'];}
					else{echo("无信息");}					?></a></td>
					<td>状态</td>
					<td><a href="#"><span style="color: #ff6600;font-weight: bold;"><?php echo $row['sell'];?></span></a></td>
				</tr>
				<!-- 第四行 -->
				<tr>
					<td>卖家电话</td>
					<td><a href=""><?php
					$con1 = "SELECT * FROM users WHERE userid='".$row['userid']."'";
					$que1 = mysqli_query($config,$con1);
					$saler = mysqli_fetch_array($que1);
					if($saler){echo $saler['uphone'];}
					else{echo("无信息");}
					?></a></td>
					<td>分类</td>
					<td><a href="#">
						<?php
						$con1 = "SELECT * FROM bigtype WHERE bigtypeid='".$row['bigtypeid']."'";
						$que1 = mysqli_query($config,$con1);
						$saler = mysqli_fetch_array($que1);
					    if($saler){echo $saler['bigtypename'];}
					    else{echo("无信息");}
					    ?></a>
					</td>
				</tr>
				<!-- 第五行 -->
				<tr>
					<td>型号码数</td>
					<td><a href="#"><?php echo $row['xinghao'];?></a>
					</td>
					<td style="font-weight: bold;font-size: 20px;">库存</td>
					<td><a href="#" style="font-size: 22px;color: #ff6600;font-weight: bold;"><?php echo $row['kucun'];?></a>
					</td>
				</tr>
				<!-- 第六行 -->
				<tr>
					<td>卖家地址</td>
					<td style="width: 87%"><a href="#" style="width: 535px">
						<?php echo $row['address'];?>
					</a></td>
				</tr>
			</table>
		</div>
		<!-- 右边信息部分结束 -->
		<!-- 购买、购物车按钮 -->
		<div class="zy_botton clearfix">
			<!-- <a class="button_order" href="http://localhost/school/order.php?key_id=<?php echo $goodskey_id;?>&sessionId=<?php echo $goodskey_id;?>">立即购买</a> -->
			<a class="button_order" href="detail.php?key_id=<?php echo $goodskey_id;?>&sessionId=<?php echo $goodskey_id;?>">
			加入购物车
			<!-- ------判断库存--------- -->
<?php
// $kucun=0;
		
				?>
		</a>
		</div>
		<!-- 查看更多板块 -->
		<div class="more">
			<a href="#top">查看更多</a>
		</div>

        
		
		<!-- 锚点 -->
		<div id="top">

			<dl class="index-list" style="width: 100%;height: 100%;margin: 0 auto;margin-top: 150px;">
				<div class="xiangqing">物品详情</div>
				<dt style="width: 20%;height: 200px;">详情介绍</dt>
				<dd style="width: 80%;height: 200px;padding-right: 20px;">
					<a href="#" style="width: 100%;padding-right: 20px;">
						<?php echo $row['intro'];?>
					</a>
				</dd>
				<dt style="width: 20%;height: 69%;">详图展示</dt>
				<dd style="width: 80%;height: 69%;">
					<a href="#" style="width: 100%;height: 69%;">
						<img style="width: 90%;height: 90%;margin-top: 10px;" src=".\images\goods\<?php echo $row['bigtypeid'];?>\<?php echo $row['smalltypeid'];?>\<?php echo $row['picture'];?>">
					</a>
				</dd>
			</dl>
		</div>
		  
	</div>
</div>



</body>
</html>